package com.vencent.spring.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vencent.spring.exception.NoPersonException;
import com.vencent.spring.exception.SaveErrorException;
import com.vencent.spring.service.Repertory;

/**
 * 你可以使用 @Component + @RequestMapping 来代替  @Controller。
 * @author 1d
 *
 */
@Controller
public class HomeController {
	@Autowired
	private Repertory rep;
	
	/**
	 * 你可以在异常上加@ResponseStatus 来给异常抛出相对应的http状态码。
	 * @return
	 */
	@RequestMapping("/my/home1")
	public String home1(){
		if(rep.select() == null){
			throw new NoPersonException();
		}
		return "home";
	}
	
	/**
	 * 使用@ExceptionHandler 自身类中捕获异常
	 * @return
	 */
	@RequestMapping("/my/home2")
	public String home2(){
		rep.save();
		return "home";
	}
	
	/**
	 * 使用@ControllerAdvice， 来集中管理异常
	 * @return
	 */
	@RequestMapping("/my/home3")
	public String home3(){
		rep.update();
		return "home";
	}
	
	
	@ExceptionHandler(SaveErrorException.class)
	public String handleException(){
		return "error";
	}
}
